package com.LinSoft.EquationSolver;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;
import android.content.*;

public class xy1solve extends Activity
{
	EditText ce_a1, ce_a2, ce_a3,
			 ce_b1, ce_b2, ce_b3;
	TextView ct_res;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.l_xy1solve);
		
		ce_a1 = (EditText)findViewById(R.id.xe_xy1_a1);
		ce_a2 = (EditText)findViewById(R.id.xe_xy1_a2);
		ce_a3 = (EditText)findViewById(R.id.xe_xy1_a3);
		ce_b1 = (EditText)findViewById(R.id.xe_xy1_b1);
		ce_b2 = (EditText)findViewById(R.id.xe_xy1_b2);
		ce_b3 = (EditText)findViewById(R.id.xe_xy1_b3);
		
		ct_res = (TextView)findViewById(R.id.xt_xy1_res);
		
		Button cb_solve = (Button)findViewById(R.id.xb_xy1_solve);
		cb_solve.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{
					solve();
				}
			});
			
		ce_b3.setOnKeyListener(new OnKeyListener()
			{
				public boolean onKey(View v, int keyCode, KeyEvent event)
				{
					if(keyCode == KeyEvent.KEYCODE_ENTER
					   && event.getAction() == KeyEvent.ACTION_DOWN)
					{
						solve();
						return true;
					}
					return false;
				}
			});
	}
	
	protected void solve()
	{
		int a1, a2, a3, b1, b2, b3, ta, tb;
		String s = getResources().getString(R.string.err_x2_noback);
		a1 = Integer.parseInt(ce_a1.getText().toString());
		a2 = Integer.parseInt(ce_a2.getText().toString());
		a3 = Integer.parseInt(ce_a3.getText().toString());
		b1 = Integer.parseInt(ce_b1.getText().toString());
		b2 = Integer.parseInt(ce_b2.getText().toString());
		b3 = Integer.parseInt(ce_b3.getText().toString());
		
		if(a1 * b2 == a2 * b1)
			s = getResources().getString(R.string.err_xy1_same);
		else
		{
			int tlcm = lcm(lcm(a1, a2), lcm(b1, b2)),
				tma, tmb, tra, trb;
			boolean neg = false;
			ta = tlcm / a2;
			tb = tlcm / b2;
			tma = a1 * ta;
			tmb = b1 * tb;
			tra = a3 * ta;
			trb = b3 * tb;
			tmb -= tma;
			trb -= tra;
			s = "X = ";
			if(tmb * trb < 0)
				s += "-";
			tma = gcd(tmb = Math.abs(tmb), trb = Math.abs(trb));
			tmb /= tma;
			trb /= tma;
			if(tmb == 1)
				s += trb;
			else
				s += "(" + trb + ")/(" + tmb + ")";
			s += "\n";
			
			neg = false;
			ta = tlcm / a1;
			tb = tlcm / b1;
			tma = a2 * ta;
			tmb = b2 * tb;
			tra = a3 * ta;
			trb = b3 * tb;
			tmb -= tma;
			trb -= tra;
			s += "Y = ";
			if(trb * tmb < 0)
				s += "-";
			tma = gcd(tmb = Math.abs(tmb), trb = Math.abs(trb));
			tmb /= tma;
			trb /= tma;
			if(tmb == 1)
				s += trb;
			else
				s += "(" + trb + ")/(" + tmb + ")";
		}
		ct_res.setText(s);
		ce_a1.requestFocus();
	}
	
	protected int gcd(int a, int b)
	{
		return b == 0 ? a : gcd(b, a % b);
	}
	
	protected int lcm(int a, int b)
	{
		return a / gcd(a, b) * b;
	}
}
